Page({
    data: {
        type: '', //0---提现至微信  1---提现至银行卡
        enableNum: null, //允许提现的金额
        bankInfo: null, //银行卡信息
        money: '',
        name: '',
        nameErr: false,
        buttonLoading: false
    },
    onLoad: function(options) {
        const type = Number(options.type)
        wx.setNavigationBarTitle({
            title: ['提现至微信', '提现至银行卡'][type]
        })
        this.setData({ type })
        const eventChannel = this.getOpenerEventChannel()
        eventChannel.on('acceptDataFromOpenerPage', ({ data }) => {
            this.setData({ enableNum: data })
        })
    },
    /**前往选择银行卡 */
    toChooseBank() {
        wx.navigateTo({
            url: '/pages/bank_manage/index?choose=true',
            events: {
                setBankInfo: ({ data }) => {
                    this.setData({ bankInfo: data })
                }
            }
        })
    },
    /**金额输入框失焦 */
    inputBlur(e) {
        let money = this.formatter(e.detail.value)
        money = this.formatter(money)
        if (money > Number(this.data.enableNum.award)) {
            money = Number(this.data.enableNum.award)
        }
        this.setData({ money })
    },
    /**验证金额输入 */
    formatter(value) {
        value = value.toString()
        value = value.replace(/[^\d.]/g, ""); //清除“数字”和“.”以外的字符
        value = value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的
        value = value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
        value = value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); //只能输入两个小数
        if (value.indexOf(".") < 0 && value != "") { //以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
            value = parseFloat(value);
        }
        value = parseFloat(value)
        if (Number.isNaN(value)) return ''
        return value
    },
    //立即提现
    withdrawal() {
        this.delay(() => {
            const { type, bankInfo, money, name } = this.data
            if (type === 1) { //银行卡
                if (bankInfo == null) return wx.showToast({
                    title: '请选择银行卡',
                    icon: 'none'
                })
            }

            if (money === 0 || money === '') return wx.showToast({
                title: '请输入提现金额',
                icon: 'none'
            })

            if (type === 0 && name === '') {
                return this.setData({ nameErr: true })
            }
            this.handle()
        }, 100)
    },
    handle() {
        this.setData({ buttonLoading: true })
        let reqData = {
            type: '1',
            from: ['weixin', 'bank'][this.data.type],
            money: this.data.money
        }
        if (this.data.type === 1) reqData.account_id = this.data.bankInfo.account_id
        reqData.account_name = this.data.type === 0 ? this.data.name : this.data.bankInfo.account_name
        this.request({
            url: '/api/user/tixian',
            method: 'POST',
            data: reqData
        }, () => {
            wx.showToast({
                title: '提现申请成功',
                icon: 'success',
                mask: true
            })
            this.delay(() => {
                wx.redirectTo({
                    url: '/pages/withdrawal_record/index'
                })
            })
        }, () => {
            this.setData({
                buttonLoading: false
            })
        })
    }
})